package controleur;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;
import modele.*;

/**
 *
 * @author Nishruu
 */
public class MainServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String type = (String)request.getParameter("type");
        System.out.println(type);
        if (type.equals("auth")) {
            String user = request.getParameter("user");
            String pwd = request.getParameter("pwd");
            User thisUser = BD.authentification(user, pwd);
            if (thisUser == null) {
                out.print("Login ou mot de passe erronés");
            } else {
                RequestDispatcher dispatch = request.getRequestDispatcher("/gantt.jsp");
                HttpSession session = request.getSession(true);
                session.setAttribute("id_user", thisUser.getId_user());
                session.setAttribute("type_user", thisUser.getType());
                dispatch.forward(request, response);
            }
        }

        else if(type.equals("getProjetsByUser"))
        {
            HttpSession session = request.getSession(true);
            int id_user = (Integer)session.getAttribute("id_user");
            String type_user = (String)session.getAttribute("type_user");
            try {
                ArrayList<Projet> projets = User.getProjetsByUser(id_user, type_user);
                HashMap<Integer, ArrayList<Equipe>> equipesByProjet = equipesByProjet = new HashMap<Integer, ArrayList<Equipe>>();
                for (int i = 0; i < projets.size(); i++) {
                     ArrayList<Equipe> equipes = Projet.getEquipes(projets.get(i).getId_projet());
                     if (equipes != null) {
                         equipesByProjet.put(projets.get(i).getId_projet(),equipes);
                     }
                }
                request.setAttribute("liste_projets", projets);
                request.setAttribute("liste_equipesByProjet", equipesByProjet);
            } catch (SQLException ex) {
                request.setAttribute("liste_projets", null);
                request.setAttribute("liste_equipesByProjet", null);
            }           
        }

        else if(type.equals("getUserInfo")){
            int id_user = Integer.parseInt(request.getParameter("id_user"));
            User user = null;
            RequestDispatcher dispatch = request.getRequestDispatcher("/profilMembre.jsp");
            try{
                user = User.getById(id_user);
                if(user!=null){
                    ArrayList<Projet> projets = User.getProjetsByUser(id_user, user.getType());
                    ArrayList<Equipe> equipes = User.getEquipesByUser(id_user, user.getType());
                    request.setAttribute ("user", user);
                    request.setAttribute("liste_projets", projets);
                    request.setAttribute("liste_equipes", equipes);
                }
                else{
                    request.setAttribute("user", null);
                    request.setAttribute("liste_projets", null);
                    request.setAttribute("liste_equipes", null);
                }
                dispatch.forward(request, response);
            }catch (SQLException ex) {
                request.setAttribute("user", null);
                request.setAttribute("liste_projets", null);
                request.setAttribute("liste_equipes", null);
                dispatch.forward(request, response);
            }
        }

        else if(type.equals("getProjetInfo"))
        {
            int id_projet = Integer.parseInt(request.getParameter("id_projet"));
            Projet projet = null;
            
            try{
                projet = Projet.getById(id_projet);
                request.setAttribute ("projet", projet);
            }catch (SQLException ex) {
                request.setAttribute("projet", null);
            }
        }

        else if(type.equals("getProjetFiche"))
        {
            int id_projet = Integer.parseInt(request.getParameter("id_projet"));
            Projet projet = null;
            ArrayList<Equipe> liste_equipes = null;
            RequestDispatcher dispatch = request.getRequestDispatcher("/ficheProjet.jsp");
            try{
                projet = Projet.getById(id_projet);
                liste_equipes = Projet.getEquipes(id_projet);
                request.setAttribute ("projet", projet);
                request.setAttribute ("liste_equipes", liste_equipes);
                dispatch.forward(request, response);
            }catch (SQLException ex) {
                request.setAttribute("projet", null);
                request.setAttribute ("liste_equipes", null);
                dispatch.forward(request, response);
            }
        }

        else if(type.equals("getEquipeInfo"))
        {
            int id_equipe = Integer.parseInt(request.getParameter("id_equipe"));
            Equipe equipe = null;
            try{
                equipe = Equipe.getById(id_equipe);
                request.setAttribute ("equipe", equipe);
            }catch (SQLException ex) {
                request.setAttribute("equipe", null);
            }
        }

        else if(type.equals("getEquipeFiche"))
        {
            int id_equipe = Integer.parseInt(request.getParameter("id_equipe"));
            Equipe equipe = null;
            Projet projet = null;
            ArrayList<Etudiant> liste_membres= null;
            RequestDispatcher dispatch = request.getRequestDispatcher("/ficheEquipe.jsp");
            try{
                equipe = Equipe.getById(id_equipe);
                projet = equipe.getProjet();
                liste_membres = equipe.getMembres();
                request.setAttribute ("equipe", equipe);
                request.setAttribute ("projet", projet);
                request.setAttribute ("liste_membres", liste_membres);
                dispatch.forward(request, response);
            }catch (SQLException ex) {
                request.setAttribute("equipe", null);
                request.setAttribute ("projet", null);
                request.setAttribute ("liste_membres", null);
                dispatch.forward(request, response);
            }
        }

        else if(type.equals("createProjet"))
        {
            int id_session = Integer.parseInt(request.getParameter("id_session"));
            request.setAttribute("id_session", id_session);
        }

        else if(type.equals("doCreateProjet"))
        {
            String nom =  (request.getParameter("nom"));
            String date_debut =  (request.getParameter("date_debut"));
            String date_fin =  (request.getParameter("date_fin"));
            int id_session = Integer.parseInt(request.getParameter("id_session"));
            try{
                Formateur formateur = ((Formateur)Formateur.getByName((request.getParameter("formateur")),"formateur").get(0));
                Projet projet = new Projet(0, nom, date_debut, date_fin, formateur, Session_diplome.getById(id_session) );
                projet.createProjet();
                request.setAttribute("projet", projet);
            }catch (SQLException ex) {
                request.setAttribute("projet", null);
            }
        }

        else if(type.equals("updateProjet"))
        {
            Projet projet;

            try {
                projet = Projet.getById(Integer.parseInt(request.getParameter("id_projet")));
                request.setAttribute("projet", projet);
            } catch (SQLException ex) {
                request.setAttribute("projet", null);
            }
        }

        else if(type.equals("doUpdateProjet"))
        {
            int id_projet = Integer.parseInt(request.getParameter("id_projet"));
            String nom = request.getParameter("nom");
            String date_debut = request.getParameter("date_debut");
            String date_fin = request.getParameter("date_fin");
            int id_session = Integer.parseInt(request.getParameter("id_session"));
            try{
                Formateur formateur = ((Formateur)Formateur.getByName((request.getParameter("formateur")),"formateur").get(0));
                Projet projet = new Projet(id_projet, nom, date_debut, date_fin,  formateur, Session_diplome.getById(id_session));
                projet.updateProjet();
                request.setAttribute("projet", projet);
            }catch (SQLException ex) {
                request.setAttribute("projet", null);
            }
        }

        else if(type.equals("suppressProjet"))
        {
            Projet projet;
            try {
                projet = Projet.getById(Integer.parseInt(request.getParameter("id_projet")));
                request.setAttribute("projet", projet);
            } catch (SQLException ex) {
                request.setAttribute("projet", null);
            }
        }

        else if(type.equals("doSuppressProjet"))
        {
            try{
                Projet projet = (Projet) (request.getAttribute("projet"));
                projet.suppressProjet();
                request.setAttribute("reponse", "Y");
            }catch (SQLException ex) {
                request.setAttribute("reponse", "N");
            }
        }

        else if(type.equals("createEquipe"))
        {
            int id_projet = Integer.parseInt(request.getParameter("id_projet"));
            request.setAttribute("id_projet", id_projet);
        }

        else if(type.equals("doCreateEquipe"))
        {
            String nom = (request.getParameter("nom"));
            String date_debut = (request.getParameter("date_debut"));
            String date_fin = (request.getParameter("date_fin"));
            int id_projet = Integer.parseInt(request.getParameter("id_projet"));
            try{
                Projet projet = Projet.getById(id_projet);
                Equipe equipe = new Equipe(0,nom,date_debut,date_fin,projet);
                equipe.createEquipe();
                request.setAttribute("reponse", "Y");
            }catch (SQLException ex) {
                request.setAttribute("reponse", "N");
            }
        }
        
        else if(type.equals("updateEquipe"))
        {
            Equipe equipe;
            try {
                equipe = Equipe.getById(Integer.parseInt(request.getParameter("id_equipe")));
                request.setAttribute("equipe", equipe);
            } catch (SQLException ex) {
                request.setAttribute("equipe", null);
            }
        }

        else if(type.equals("doUpdateEquipe"))
        {
            int id_equipe = Integer.parseInt(request.getParameter("id_equipe"));
            String nom = (request.getParameter("nom"));
            String date_debut = (request.getParameter("date_debut"));
            String date_fin = (request.getParameter("date_fin"));
            int id_projet = Integer.parseInt(request.getParameter("id_projet"));
            try{
                Equipe equipe = new Equipe(id_equipe, nom, date_debut, date_fin, Projet.getById(id_projet));
                equipe.updateEquipe();
                request.setAttribute("reponse", "Y");
            }catch (SQLException ex) {
                request.setAttribute("reponse", "N");
            }
        }

        else if(type.equals("doAddMembretoEquipe"))
        {
            int id_equipe = Integer.parseInt(request.getParameter("id_equipe"));
            int id_membre = Integer.parseInt(request.getParameter("id_membre"));
            try{
                Equipe.getById(id_equipe).addMembre(id_membre);
                request.setAttribute("reponse", "Y");
            }catch (SQLException ex) {
                request.setAttribute("reponse", "N");
            }
        }

        else if(type.equals("addMembretoEquipe"))
        {
            List<Etudiant> liste_etudiant;
            int id_equipe = Integer.parseInt(request.getParameter("id_equipe"));
            try {
                liste_etudiant = Etudiant.getAll();
                request.setAttribute("liste_etudiant", liste_etudiant);
                request.setAttribute("id_equipe", id_equipe);
            } catch (SQLException ex) {
                request.setAttribute("liste_etudiant", null);
                request.setAttribute("id_equipe", null);
            }
        }
        
        else if(type.equals("suppressEquipe")) {
            
            try{
                Equipe equipe = Equipe.getById(Integer.parseInt(request.getParameter("id")));
                request.setAttribute("equipe", equipe);
            }catch (SQLException ex) {
                request.setAttribute("equipe", null);
            }
        }

        else if(type.equals("doSuppressEquipe")) {

            try{
                Equipe equipe = Equipe.getById(Integer.parseInt(request.getParameter("id")));
                equipe.suppressEquipe();
                request.setAttribute("reponse", "Y");
            }catch (SQLException ex) {
                request.setAttribute("reponse", "N");
            }
        }

        else if(type.equals("monProfil")) {
            HttpSession session = request.getSession(true);
            int id_user = (Integer)session.getAttribute("id_user");
            String type_user = (String)session.getAttribute("type_user");

            User user = null;
            try{
                user = User.getById(id_user, type_user);
                ArrayList<Projet> projets = User.getProjetsByUser(id_user, type_user);
                ArrayList<Equipe> equipes = User.getEquipesByUser(id_user, type_user);
                request.setAttribute ("user", user);
                request.setAttribute("liste_projets", projets);
                request.setAttribute("liste_equipes", equipes);
            }catch (SQLException ex) {
                request.setAttribute("user", null);
                request.setAttribute("liste_projets", null);
                request.setAttribute("liste_equipes", null);
            }
        }
        
        else if(type.equals("deconnexion")) {
            HttpSession session = request.getSession(true);
            session.setAttribute("id_user", null);
            session.setAttribute("type_user", null);
            RequestDispatcher dispatch = request.getRequestDispatcher("/index.jsp");
            dispatch.forward(request, response);
        }

        else if(type.equals("rechercher")) {
            RequestDispatcher dispatch = request.getRequestDispatcher("rechercher.jsp");
            String keyword = (String) (request.getParameter("keyword"));
            ArrayList<User> listeEtudiant = null;
            ArrayList<User> listeFormateur = null;
            ArrayList<Projet> listeProjet = null;
            ArrayList<Equipe> listeEquipe = null;
            try {
                listeEtudiant = (User.getByName(keyword, "etudiant"));
                listeFormateur = (User.getByName(keyword, "formateur"));
                listeProjet = Projet.getByName(keyword);
                listeEquipe = Equipe.getByName(keyword);
                request.setAttribute("liste_etudiants", listeEtudiant);
                request.setAttribute("liste_formateurs", listeFormateur);
                request.setAttribute("liste_projets", listeProjet);
                request.setAttribute("liste_equipes", listeEquipe);
                dispatch.forward(request, response);
            } catch (SQLException ex) {
                request.setAttribute("liste_etudiants", null);
                request.setAttribute("liste_formateurs", null);
                request.setAttribute("liste_projets", null);
                request.setAttribute("liste_equipes", null);
                dispatch.forward(request, response);
            }

        }
    }

    /**
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }



    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
   
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
